package testcase;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import game.*;

import player.Player;
import card.Cardset;
import card.Card;
import io.IOManager;

import junit.framework.TestCase;

public class GameTurnTest extends TestCase {
	
		
	class PlayerStub extends Player
	{
		private Cardset[] startTurnCardSet;		
		private int startTurnCount;
		
		
		public PlayerStub(Cardset[] startTurnCardSet)
		{
			super("",0,null);
			this.startTurnCardSet = startTurnCardSet;			
		}
				
		public Cardset startTurn()
		{
			return startTurnCardSet[startTurnCount++];			
		}
		
		public void endTurn(){	}
		
		
	}
	
	class GameStub extends Game
	{
		public boolean bAccept;		
		
		public GameStub()
		{
			super(null,null);	
			bAccept = false;
		}
		
		public boolean acceptPlay(Cardset play)
		{
			bAccept = !bAccept;
			return bAccept;	
		}		
	}
	
	class IOManagerStub extends IOManager
	{
		private String input[];
		private int inputCount;
		
		IOManagerStub(String[] input)
		{
			this.input = input;
		}
		
		public char ioConfirmPlay(Cardset play)
		{
			return input[inputCount++].charAt(0);
		}
		
				
	}
	
	
	public void setUp(){ }	
	public void tearDown() { }
	
	public void testSkipTurn()
	{
		Cardset cardsSelections[];
		cardsSelections = new Cardset[1];
		cardsSelections[0] = new Cardset(0);
		
		PlayerStub player = new PlayerStub(cardsSelections);
		
		String input[];
		input = new String[1];
		input[0] = "c";
		               
		IOManager ioManager = new IOManagerStub(input);
		GameStub game = new GameStub();
		
		GameTurn gameTurn = new GameTurn(player);
				
		TurnResult result = gameTurn.play(ioManager, game);
		
		assertEquals(TurnResult.SKIPPED, result);		
	}
	
	public void testTurnConfirmed()
	{
		Cardset cardsSelections[];
		cardsSelections = new Cardset[1];
		cardsSelections[0] = new Cardset(1);
		
		cardsSelections[0].add(new Card('s','4'));
		PlayerStub player = new PlayerStub(cardsSelections);
		
		String input[];
		input = new String[1];
		input[0] = "c";
		               
		IOManagerStub ioManager = new IOManagerStub(input);
		
		GameStub game = new GameStub();		
		GameTurn gameTurn = new GameTurn(player);				
		TurnResult result = gameTurn.play(ioManager, game);		
		assertEquals(TurnResult.CONFIRMED, result);		
	}
	
	
	public void testResetSelection()
	{
		Cardset cardsSelections[];
		cardsSelections = new Cardset[3];
		cardsSelections[0] = new Cardset(1);
		cardsSelections[1] = new Cardset(0);
		cardsSelections[2] = new Cardset(0);
		
		cardsSelections[0].add(new Card('s','4'));
		
		PlayerStub player = new PlayerStub(cardsSelections);
		
		String input[];
		input = new String[2];
		input[0] = "r";
		input[1] = "c";
				
		               
		IOManagerStub ioManager = new IOManagerStub(input);
		
		GameStub game = new GameStub();		
		GameTurn gameTurn = new GameTurn(player);				
		TurnResult result = gameTurn.play(ioManager, game);		
		assertEquals(TurnResult.SKIPPED, result);					
	}
	
	public void testRejectSelection()
	{
		Cardset cardsSelections[];
		cardsSelections = new Cardset[3];
		cardsSelections[0] = new Cardset(1);
		cardsSelections[1] = new Cardset(1);
		cardsSelections[2] = new Cardset(0);
		
		
		cardsSelections[0].add(new Card('s','5'));
		cardsSelections[1].add(new Card('s','4'));
				
		PlayerStub player = new PlayerStub(cardsSelections);
		
		String input[];
		input = new String[3];
		input[0] = "c";
		input[1] = "c";
		input[2] = "c";
		System.setOut(new PrintStream(new ByteArrayOutputStream()));		               
		IOManagerStub ioManager = new IOManagerStub(input);
		
		GameStub game = new GameStub();		
		GameTurn gameTurn = new GameTurn(player);				
		TurnResult result = gameTurn.play(ioManager, game);
		TurnResult result2 = gameTurn.play(ioManager, game);
		
		assertEquals(TurnResult.CONFIRMED, result);
		assertEquals(TurnResult.SKIPPED, result2);
		
		
	}
	
	

}
